/**
 * Created by sail on 2017/6/1.
 */
import WeCropper from '../we-cropper/we-cropper.js'

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight-50 ;

Page({
  data: {
    cropperOpt: {
      id: 'cropper',
      width,
     height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 300,
        height: 300
      }
    }
  },
  touchStart (e) {
    this.wecropper.touchStart(e)
  },
  touchMove (e) {
    this.wecropper.touchMove(e)
  },
  touchEnd (e) {
    this.wecropper.touchEnd(e)
  },
  //获取剪裁后的图片
  getCropperImage () {
    var that = this;

    this.wecropper.getCropperImage((src) => {
      if (src) {
        console.log(src)
        wx.previewImage({
          current: '', // 当前显示图片的http链接
          urls: [src] // 需要预览的图片http链接列表
        })
        var tempFilePaths = src;
        console.log(tempFilePaths);
        //org_avatar_preview = tempFilePaths;
        wx.setStorageSync('tempFilePaths', tempFilePaths)
        //是否使用图片,点击使用图片按钮后，cutFlag为true，头像的缩略图加载成新的
        wx.setStorageSync("cutFlag", true);

        wx.navigateBack({

        })

      } else {
        console.log('获取图片地址失败，请稍后重试')
        wx.showModal({
          title: '使用失败',
          content: '获取图片地址失败，请稍后重试',
          confirmText:"重试",
          success:function(res){
            if(res.cancel){
              wx.navigateBack({
                
              })
            }else{
              that.getCropperImage
            }
          }
        })
      }
    })
  },
  // 已经不用
  uploadTap () {
    const self = this

    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success (res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值

        self.wecropper.pushOrign(src)
      }
    })
  },
  onLoad (option) {
    const { cropperOpt } = this.data

    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas();
    //是否使用图片,如果没有使用图片直接返回，择cutFlag为false，头像的缩略图不变成新的
    wx.setStorageSync("cutFlag", false);
    const src = wx.getStorageSync("tempFilePaths");
    const self = this;
    //剪裁图片
    self.wecropper.pushOrign(src)
  
  },

})
